//
//  UIImage+ALImage.m
//  ALUtils_Example
//
//  Created by hcs on 2018/8/2.
//  Copyright © 2018年 anlun. All rights reserved.
//

#import "UIImage+ALImage.h"

@implementation UIImage (ALImage)
/**
 绘制图片
 
 @param size         规定绘制而成的图片大小
 @param fillColor    背景颜色
 @param cornerRadio  圆角大小
 @param completion   完成回调
 */
- (void)my_ImageWithSize:(CGSize)size fillColor:(UIColor *)fillColor cornerRadio:(CGFloat)cornerRadio completion:(void (^)(UIImage *))completion{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
        CGSize aspectFitSize = size;
        CGRect rect = CGRectMake(0, 0, aspectFitSize.width+0.3, aspectFitSize.height+0.3);
        [fillColor setFill];
        UIRectFill(rect);
        if (cornerRadio != 0) {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, aspectFitSize.width, aspectFitSize.height) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
            [path addClip];
        }
        [self drawInRect:rect];
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion != nil) {
                if (result) {
                    completion(result);
                }
            }
        });
    });
}

@end
